
<template>
  <!-- 发布资讯页面 -->
  <div>
    <el-form ref="myform" :model="form" :rules="rule" label-width="80px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="form.title"
          class="input"
          autocomplete="off"
          clearable
        >
        </el-input>
      </el-form-item>

      <el-form-item label="分类" prop="categoryId">
        <el-select v-model="form.categoryId" placeholder="请选择">
          <el-option
            clearable
            v-for="item in list"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        :label-width="formLabelWidth"
        prop="cover"
        class="photo"
        label="封面"
      >
        <el-upload
          class="avatar-uploader"
          :action="uploadFileURL"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="form.cover" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <p>文件大小不允许超过3M</p>
      </el-form-item>
      <!-- 富文本框 -->
      <el-form-item class="tinymce" label="正文" prop="content">
        <Tinymce v-model="form.content" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="$router.go(-1)">返回</el-button>
      <el-button type="primary" @click="toSubmit">发 布</el-button>
    </span>
  </div>
</template>

<script>
import { cateGory } from "@/api/proCategory";
import { uploadFileURL } from "@/utils/config.js";
import Tinymce from "../../../../components/Tinymce";
import { saveOrUpdate } from "@/api/article";
export default {
  components: {
    Tinymce,
  },
  data() {
    return {
      form: {},
      list: [],
      formLabelWidth: "120px",
      uploadFileURL,
      imageUrl: "",
      rule: {
        title: [{ required: true, message: "请输入文章标题", trigger: "blur" }],
        categoryId: [
          { required: true, message: "请选择栏目", trigger: "change" },
        ],
        content: [
          { required: true, message: "请输入文章内容", trigger: "blur" },
        ],
      },
    };
  },
  computed: {},
  methods: {
    async getList() {
      let res = await cateGory();
      this.list = res.data;
    },
    // 图片上传成功的方法
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.cover = res.data;
    },
    // 图片上传之前
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    toSubmit() {
      // 表单验证
      this.$refs.myform.validate(async (valid) => {
        if (valid) {
          // 发送保存或更新请求
          let res = await saveOrUpdate(this.form);
          // 更新成功，跳转页面
          this.$router.push({
            path: "List",
          });
        } else {
          this.$message({
            type: "error",
            message: "请输入必传字段",
          });
        }
      });
    },
  },
  created() {
    this.getList();
  },
  mounted() {},
};
</script>
<style scoped>
.input {
  display: inline-block;
  width: 700px;
}
.photo {
  position: absolute;
  top: 10px;
  right: 100px;
}
.tinymce {
  width: 95%;
  margin-top: 200px;
  margin-left: -20px;
}
.dialog-footer {
  float: right;
  margin-right: 100px;
}
</style>